package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.adapter.DinsAdapter;
import com.jinglvhuanjing.app.bean.DinSwitchBean;
import com.jinglvhuanjing.app.listener.SwitchEvent;
import com.jinglvhuanjing.app.utils.ByteUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SignalOutPanelItemView extends LinearLayoutCompat {

    private static final String TAG = "common";

    public SignalOutPanelItemView(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public SignalOutPanelItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public SignalOutPanelItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    /*** 信号投口label ***/
    private AppCompatTextView signal_out_panel_item_label;
    /*** 信号投口列表 ***/
    private RecyclerView mDinsListView;
    private DinsAdapter mDinsAdapter;
    private String mLabel;
    private String mKey;
    private SwitchEvent mSwitchEvent;

    private void init(Context context) {
        setGravity(Gravity.CENTER_VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.view_signal_out_panel_item_layout, this, true);
        this.signal_out_panel_item_label = findViewById(R.id.signal_out_panel_item_label);
        this.mDinsListView = findViewById(R.id.signal_out_panel_item_dins);
        this.mDinsListView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        this.mDinsAdapter = new DinsAdapter();
        this.mDinsListView.setAdapter(this.mDinsAdapter);
        this.initEvent();
        this.testData();
    }

    private void initEvent() {
        this.mDinsAdapter.setOnItemClickListener((adapter, view, position) -> {
            DinSwitchBean switchBean = this.mDinsAdapter.getItem(position);
            boolean isChecked = switchBean.isChecked();
            switchBean.setChecked(!isChecked);
            this.mDinsAdapter.notifyItemChanged(position);
            if (this.mSwitchEvent == null) {
                return;
            }
            int tkIndex = position + 1;
            Log.d(TAG, String.format("开关切换： LABEL = %s , KEY = %s_%s , SWITCH_STATUS = %s", mLabel, mKey, tkIndex, switchBean.isChecked()));
            this.mSwitchEvent.switchCallBack(mLabel, String.format("%s_%s", mKey, tkIndex), switchBean.isChecked());

            ArrayList<DinSwitchBean> data = (ArrayList<DinSwitchBean>) mDinsAdapter.getData();
            StringBuffer buffer = new StringBuffer();
            if(data.size()<4){
                buffer.append("0");
            }
            for(int i=data.size()-1; i>=0; i--){
                DinSwitchBean switchBean_ = data.get(i);
                boolean isChecked_ = switchBean_.isChecked();
                if(isChecked_){
                    buffer.append("1");
                }else{
                    buffer.append("0");
                }
            }
            this.mSwitchEvent.switchCallBack_(mLabel, mKey, ByteUtil.String_0(ByteUtil.binaryToHex(buffer.toString()), 2));
        });
    }

    public void setLabel(String label) {
        if (TextUtils.isEmpty(label)) {
            return;
        }
        this.signal_out_panel_item_label.setText(label);
    }

    private void testData() {
        ArrayList<DinSwitchBean> items = new ArrayList<>();
        DinSwitchBean bean;
        for (int i = 0; i < 3; i++) {
            bean = new DinSwitchBean();
            bean.setName(String.format("DOUT%s", i + 1));
            items.add(bean);
        }
        this.mDinsAdapter.replaceData(items);
    }


    public void setParamTag(String label, String key) {
        this.mLabel = label;
        this.mKey = key;
    }

    /**
     * 获取值
     *
     * @return 集合
     */
    public HashMap<String, String> getFormValue() {
        List<DinSwitchBean> dinSwitchBeans = mDinsAdapter.getData();
        HashMap<String, String> dinSwitchParamMap = new HashMap<>();
        int index = 1;
        for (DinSwitchBean dinSwitchBean : dinSwitchBeans) {
            dinSwitchParamMap.put(String.format("%s_%s", mKey, index), dinSwitchBean.isChecked() ? "1" : "0");
            index++;
        }
        return dinSwitchParamMap;
    }

    public void addSwitchEvent(SwitchEvent switchEvent) {
        this.mSwitchEvent = switchEvent;
    }
}
